﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;

namespace BYManageInfoSystem
{
    public partial class Frm_RecvMoneyDetailSheet : Form
    {
        private DGBY.BLL.BYCompanyTab m_bllBYCompanyTab = new DGBY.BLL.BYCompanyTab();
        private DGBY.BLL.BYDepartTab m_bllBYDepartTab = new DGBY.BLL.BYDepartTab();
        private DGBY.BLL.FactoryDataTab m_bllFactoryDataTab = new DGBY.BLL.FactoryDataTab();
        private DGBY.BLL.FinRecvMoneyWayTab m_bllFinRecvMoneyWayTab = new DGBY.BLL.FinRecvMoneyWayTab();
        private DGBY.BLL.BiZhiTab m_bllBiZhiTab = new DGBY.BLL.BiZhiTab();
        private DGBY.BLL.FinRecvMoneyDetailTab m_bllFinRecvMoneyDetailTab = new DGBY.BLL.FinRecvMoneyDetailTab();
        private int m_nSelectFinRecvMoneyDetailTabID = 0;
        private Dictionary<string, string> dicQueryCondition = new Dictionary<string, string>();

        string m_strQueryWhere = "";
        private bool IsDecimal(string strInput)
        {
            bool bRet = Regex.IsMatch(strInput, @"([1-9]+[0-9]*(\.[0-9]+))|(0(\.[0-9]+))");
            return bRet;
        }

        private bool IsNum(string strInput)
        {
            bool bRet = Regex.IsMatch(strInput, @"^[1-9]+\d*$");
            return bRet;
        }

        private bool ValidDate(string strInput)
        {
            if (strInput.Length != 8)
                return false;
            else
                return IsNum(strInput);
        }

        public bool IsDecimal(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsDecimal(TextBox.Text) && !IsNum(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public bool IsNum(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsNum(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        public bool IsNull(ErrorProvider errorProvider1, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }
        public void InitUltraWinGridSetting()
        {
            this.ultraGrid1.DisplayLayout.Override.RowSelectors = Infragistics.Win.DefaultableBoolean.True;
            this.ultraGrid1.DisplayLayout.Override.RowSelectorNumberStyle = Infragistics.Win.UltraWinGrid.RowSelectorNumberStyle.VisibleIndex;
        }

        public Frm_RecvMoneyDetailSheet()
        {
            InitializeComponent();
        }
        public void LocalPrintPreveiw()
        {
            Infragistics.Shared.ResourceCustomizer rc = Infragistics.Win.Printing.Resources.Customizer;//Resources.Customizer;
            rc.SetCustomizedString("PrintPreview_DialogCaption", "打印预览");
            rc.SetCustomizedString("PrintPreview_Tool_Print", "打印(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_ClosePreview", "关闭(&C)");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuPreviewZoom", "显示比例");
            rc.SetCustomizedString("PrintPreview_Tool_Current_Page", "当前页");
            rc.SetCustomizedString("PrintPreview_Tool_Exit", "退出(&X)");
            rc.SetCustomizedString("PrintPreview_Tool_First_Page", "第一页");
            rc.SetCustomizedString("PrintPreview_Tool_Go_To", "跳至");
            rc.SetCustomizedString("PrintPreview_Tool_Last_Page", "最后一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_Page", "下一页");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_Page", "前一页");
            rc.SetCustomizedString("PrintPreview_Tool_Next_View", "下一视图(&N)");
            rc.SetCustomizedString("PrintPreview_Tool_Previous_View", "前一视图(&P)");
            rc.SetCustomizedString("PrintPreview_Tool_Hand_Tool", "手型工具(&H)");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Setup", "页面设置(&U)");
            rc.SetCustomizedString("PrintPreview_Tool_Snapshot_Tool", "快照工具(&S)");
            rc.SetCustomizedString("PrintPreview_Tool_View", "视图(&V)");
            rc.SetCustomizedString("PrintPreview_Tool_Whole_Page", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom", "缩放(&Z)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In", "放大");

            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out", "缩小");

            rc.SetCustomizedString("PrintPreview_ToolCategory_Context_Menus", "上下文菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_File", "文件");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Menus", "菜单");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Tools", "工具栏");
            rc.SetCustomizedString("PrintPreview_ToolCategory_View", "视图");
            rc.SetCustomizedString("PrintPreview_ToolCategory_Zoom_Mode", "缩放模式");

            rc.SetCustomizedString("PrintPreview_ToolTip_ClosePreview", "关闭");
            rc.SetCustomizedString("PrintPreview_ToolTip_Zoom", "缩放");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "页:{0}/{1}");

            rc.SetCustomizedString("CustomizeImg_ToolBar_MenuBar", "菜单");
            rc.SetCustomizedString("CustomizeImg_ToolBar_Standard", "标准");
            rc.SetCustomizedString("CustomizeImg_ToolBar_View", "视图");
            rc.SetCustomizedString("PrintPreview_Tool_File", "文件(&F)");
            rc.SetCustomizedString("PrintPreview_Tool_Tools", "工具(&T)");
            rc.SetCustomizedString("PrintPreview_Tool_Dynamic_Zoom_Tool", "动态缩放工具(&D)");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_Out_Tool", "缩小工具");
            rc.SetCustomizedString("PrintPreview_Tool_Zoom_In_Tool", "放大工具");
            // rc.SetCustomizedString("PrintPreview_Tool_Page_Layout","菜单");
            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PreviewRowColSelection_SelectedPages", "{0} x {1} 页");

            rc.SetCustomizedString("PreviewRowColSelection_Cancel", "取消");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Width", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_MarginWidth", "文字宽度");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_PageWidth", "页宽");
            rc.SetCustomizedString("PrintPreview_ZoomListItem_WholePage", "合适页");
            rc.SetCustomizedString("PrintPreview_Tool_Page_Layout", "页面布局");
            rc.SetCustomizedString("PrintPreview_Tool_Margin_Width", "文字宽度");
            rc.SetCustomizedString("ContextMenuPreviewHand", "缩放视图");


            rc.SetCustomizedString("PrintPreview_Tool_Reduce_Page_Thumbnails", "缩小");
            rc.SetCustomizedString("PrintPreview_Tool_Show_Page_Numbers", "显示页号");
            rc.SetCustomizedString("PrintPreview_Tool_ContextMenuThumbnail", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Enlarge_Page_Thumbnails", "放大");
            rc.SetCustomizedString("PrintPreview_Tool_Thumbnails", "缩略图");
            rc.SetCustomizedString("PrintPreview_Tool_Continuous", "连续排序");
            //说明
            rc.SetCustomizedString("StatusBar_DynamicZoom_Instructions", "单击并拖动进行缩放操作");
            rc.SetCustomizedString("StatusBar_Page_X_OF_X", "当前页: {0} / {1}");
            rc.SetCustomizedString("StatusBar_SnapShot_Instructions", "单击并拖动,系统将选定矩型区域复制到剪帖板");
            rc.SetCustomizedString("StatusBar_ZoomIn_Instructions", "单击并拖动,系统将放大选定矩型区域");
            rc.SetCustomizedString("StatusBar_ZoomOut_Instructions", "单击并拖动,系统将缩小选定矩型区域");
            rc.SetCustomizedString("StatusBar_Hand_Instructions", "单击并拖动以便显示更多内容");
        }
        private void InitCombox()
        {
            DataSet ds = m_bllBYCompanyTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxBYComp.DataSource = ds.Tables[0];
                this.comboBoxBYComp.DisplayMember = "经营单位名称";
                this.comboBoxBYComp.ValueMember = "BYCompanyTabID";
                this.comboBoxBYComp.SelectedIndex = 0;
            }

            ds = m_bllBYDepartTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDept.DataSource = ds.Tables[0];
                this.comboBoxDept.DisplayMember = "部门名称";
                this.comboBoxDept.ValueMember = "BYDepartTabID";
                this.comboBoxDept.SelectedIndex = 0;
            }

            ds = m_bllFactoryDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxFacName.DataSource = ds.Tables[0];

                this.comboBoxFacName.ValueMember = "FactoryDataTabID";
                this.comboBoxFacName.DisplayMember = "工厂名称";
                this.comboBoxFacName.SelectedIndex = 0;
            }

            ds = m_bllFinRecvMoneyWayTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxRecvMoneyWay.DataSource = ds.Tables[0];

                this.comboBoxRecvMoneyWay.ValueMember = "FinRecvMoneyWayTabID";
                this.comboBoxRecvMoneyWay.DisplayMember = "收款方式";
                this.comboBoxRecvMoneyWay.SelectedIndex = 0;
            }

            ds = m_bllBiZhiTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxBiZhong.DataSource = ds.Tables[0];

                this.comboBoxBiZhong.ValueMember = "BiZhiTabID";
                this.comboBoxBiZhong.DisplayMember = "币制名称";
                this.comboBoxBiZhong.SelectedIndex = 0;
            }
            ds = m_bllBiZhiTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxZheSBiZhong.DataSource = ds.Tables[0];

                this.comboBoxZheSBiZhong.ValueMember = "BiZhiTabID";
                this.comboBoxZheSBiZhong.DisplayMember = "币制名称";
                this.comboBoxZheSBiZhong.SelectedIndex = 0;
            }

        }

        private void Frm_RecvMoneyDetailSheet_Load(object sender, EventArgs e)
        {
            InitCombox();
            LocalPrintPreveiw();
            InitUltraWinGridSetting();
            InitQueryCondition();
        }

        private void ShowAllRecord(string strWhere)
        {
            ultraGrid1.DataSource = null;
            DataSet ds = m_bllFinRecvMoneyDetailTab.ShowRecvMoneyDetailRec(strWhere);

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                ultraGrid1.DataSource = ds.Tables[0];
                ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            }
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }

        private string MakeBianHao(DateTime dtRecvMoneyDate)
        {
            int nFinRecvMoneyWayTabID = 0;
            if (this.comboBoxRecvMoneyWay.SelectedValue != null)
                nFinRecvMoneyWayTabID = Convert.ToInt32(this.comboBoxRecvMoneyWay.SelectedValue.ToString(), 10);
            DataSet ds = m_bllFinRecvMoneyWayTab.GetList("FinRecvMoneyWayTabID = " + nFinRecvMoneyWayTabID.ToString());
            string strFinRecvMoneyWay = "";
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                strFinRecvMoneyWay = ds.Tables[0].Rows[0]["代码"].ToString();
            }
            string strDate = dtRecvMoneyDate.Year.ToString() + dtRecvMoneyDate.Month.ToString().PadLeft(2, '0') + dtRecvMoneyDate.Day.ToString().PadLeft(2, '0');
            ds = m_bllFinRecvMoneyDetailTab.GetBianHaoByDateAndRecvMoneyWay("RecvMoneyDate = '" + strDate + "'" + "and FinRecvMoneyWayTabID = " + nFinRecvMoneyWayTabID.ToString());
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                string strDJNo = ds.Tables[0].Rows[0]["MAXBianHao"].ToString();
                int nIndex = strDJNo.IndexOf(strFinRecvMoneyWay) + 1;
                long nDJNo = 0;
                if (strDJNo != "")
                {
                    string strConvert = strDJNo.Substring(nIndex, 4);
                    nDJNo = Convert.ToInt32(strConvert, 10);
                }
                nDJNo++;
                string strTemp = strDate + strFinRecvMoneyWay + string.Format("{0:d4}", nDJNo);

                return strTemp;
            }

            return "";
        }


        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxRecvMoneyJinE))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxZheSHuiLv))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxMarkedPrice))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxDiscount))
                    return;


                DGBY.Model.FinRecvMoneyDetailTab modelFinRecvMoneyDetailTab = new DGBY.Model.FinRecvMoneyDetailTab();

                //modelFinRecvMoneyDetailTab.SenquenceNo = this.textBoxSeqNo.Text;
                modelFinRecvMoneyDetailTab.BianHao = MakeBianHao(this.dateTimePickerRecvMoneyDate.Value.Date);
                modelFinRecvMoneyDetailTab.RecvMoneyValue = Convert.ToDecimal(this.textBoxRecvMoneyJinE.Text);
                modelFinRecvMoneyDetailTab.ZheSuanHuiLv = Convert.ToDecimal(this.textBoxZheSHuiLv.Text);
                modelFinRecvMoneyDetailTab.Paijia = Convert.ToDecimal(this.textBoxMarkedPrice.Text);
                modelFinRecvMoneyDetailTab.YouHuiJia = Convert.ToDecimal(this.textBoxDiscount.Text);
                modelFinRecvMoneyDetailTab.PayPerson = this.textBoxPayPerson.Text;

                DataSet ds = m_bllFinRecvMoneyDetailTab.GetSeqNo("");
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    string strMAXSenquenceNo = ds.Tables[0].Rows[0]["MAXSenquenceNo"].ToString();
                    int nSeqNo = 1;
                    if (strMAXSenquenceNo != "")
                        nSeqNo = Convert.ToInt32(strMAXSenquenceNo, 10) + 1;
                    modelFinRecvMoneyDetailTab.SenquenceNo = nSeqNo;
                }

                modelFinRecvMoneyDetailTab.PaijiaJinE = Convert.ToDecimal(modelFinRecvMoneyDetailTab.RecvMoneyValue) * Convert.ToDecimal(modelFinRecvMoneyDetailTab.Paijia);
                modelFinRecvMoneyDetailTab.YouHuiJiaJinE = Convert.ToDecimal(modelFinRecvMoneyDetailTab.RecvMoneyValue) * Convert.ToDecimal(modelFinRecvMoneyDetailTab.YouHuiJia);


                if (this.comboBoxBiZhong.Text == "港币")
                {
                    modelFinRecvMoneyDetailTab.ZheSuanValue = Convert.ToDecimal(modelFinRecvMoneyDetailTab.RecvMoneyValue) * Convert.ToDecimal(modelFinRecvMoneyDetailTab.ZheSuanHuiLv);
                }
                else
                {
                    modelFinRecvMoneyDetailTab.ZheSuanValue = Convert.ToDecimal(modelFinRecvMoneyDetailTab.PaijiaJinE) / Convert.ToDecimal(modelFinRecvMoneyDetailTab.ZheSuanHuiLv);
                }



                if (this.comboBoxBYComp.SelectedValue != null)
                    modelFinRecvMoneyDetailTab.BYCompanyTabID = Convert.ToInt32(this.comboBoxBYComp.SelectedValue.ToString(), 10);
                if (this.comboBoxDept.SelectedValue != null)
                    modelFinRecvMoneyDetailTab.BYDepartTabID = Convert.ToInt32(this.comboBoxDept.SelectedValue.ToString(), 10);
                if (this.comboBoxFacName.SelectedValue != null)
                    modelFinRecvMoneyDetailTab.FactoryDataTabID = Convert.ToInt32(this.comboBoxFacName.SelectedValue.ToString(), 10);
                if (this.comboBoxRecvMoneyWay.SelectedValue != null)
                    modelFinRecvMoneyDetailTab.FinRecvMoneyWayTabID = Convert.ToInt32(this.comboBoxRecvMoneyWay.SelectedValue.ToString(), 10);
                if (this.comboBoxBiZhong.SelectedValue != null)
                    modelFinRecvMoneyDetailTab.BiZhiTabID = Convert.ToInt32(this.comboBoxBiZhong.SelectedValue.ToString(), 10);
                if (this.comboBoxZheSBiZhong.SelectedValue != null)
                    modelFinRecvMoneyDetailTab.ZheSuanBiZhiTabID = Convert.ToInt32(this.comboBoxZheSBiZhong.SelectedValue.ToString(), 10);

                modelFinRecvMoneyDetailTab.RecvMoneyDate = this.dateTimePickerRecvMoneyDate.Value.Date;
                modelFinRecvMoneyDetailTab.RenLingDate = this.dateTimePickerConfirmDate.Value.Date;


                m_bllFinRecvMoneyDetailTab.Add(modelFinRecvMoneyDetailTab);
                ShowAllRecord("");
                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
           
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            //ultraGrid1.DisplayLayout.Override.RowSizing = RowSizing.AutoFree;
            //foreach (UltraGridColumn ugc in ultraGrid1.DisplayLayout.Bands[0].Columns)
            //    ugc.PerformAutoResize();
            // FILTER ROW FUNCTIONALITY RELATED ULTRAGRID SETTINGS
            // ----------------------------------------------------------------------------------
            // Enable the the filter row user interface by setting the FilterUIType to FilterRow.
            e.Layout.Override.FilterUIType = FilterUIType.FilterRow;

            // FilterEvaluationTrigger specifies when UltraGrid applies the filter criteria typed 
            // into a filter row. Default is OnCellValueChange which will cause the UltraGrid to
            // re-filter the data as soon as the user modifies the value of a filter cell.
            e.Layout.Override.FilterEvaluationTrigger = FilterEvaluationTrigger.OnCellValueChange;

            // By default the UltraGrid selects the type of the filter operand editor based on
            // the column's DataType. For DateTime and boolean columns it uses the column's editors.
            // For other column types it uses the Combo. You can explicitly specify the operand
            // editor style by setting the FilterOperandStyle on the override or the individual
            // columns.
            //e.Layout.Override.FilterOperandStyle = FilterOperandStyle.Combo;

            // By default UltraGrid displays user interface for selecting the filter operator. 
            // You can set the FilterOperatorLocation to hide this user interface. This
            // property is available on column as well so it can be controlled on a per column
            // basis. Default is WithOperand. This property is exposed off the column as well.
            e.Layout.Override.FilterOperatorLocation = FilterOperatorLocation.WithOperand;

            // By default the UltraGrid uses StartsWith as the filter operator. You use
            // the FilterOperatorDefaultValue property to specify a different filter operator
            // to use. This is the default or the initial filter operator value of the cells
            // in filter row. If filter operator user interface is enabled (FilterOperatorLocation
            // is not set to None) then that ui will be initialized to the value of this
            // property. The user can then change the operator as he/she chooses via the operator
            // drop down.
            e.Layout.Override.FilterOperatorDefaultValue = FilterOperatorDefaultValue.StartsWith;

            // FilterOperatorDropDownItems property can be used to control the options provided
            // to the user for selecting the filter operator. By default UltraGrid bases 
            // what operator options to provide on the column's data type. This property is
            // avaibale on the column as well.
            //e.Layout.Override.FilterOperatorDropDownItems = FilterOperatorDropDownItems.All;

            // By default UltraGrid displays a clear button in each cell of the filter row
            // as well as in the row selector of the filter row. When the user clicks this
            // button the associated filter criteria is cleared. You can use the 
            // FilterClearButtonLocation property to control if and where the filter clear
            // buttons are displayed.
            e.Layout.Override.FilterClearButtonLocation = FilterClearButtonLocation.RowAndCell;

            // Appearance of the filter row can be controlled using the FilterRowAppearance proeprty.
            e.Layout.Override.FilterRowAppearance.BackColor = Color.LightYellow;

            // You can use the FilterRowPrompt to display a prompt in the filter row. By default
            // UltraGrid does not display any prompt in the filter row.
            e.Layout.Override.FilterRowPrompt = "Click here to filter data...";

            // You can use the FilterRowPromptAppearance to change the appearance of the prompt.
            // By default the prompt is transparent and uses the same fore color as the filter row.
            // You can make it non-transparent by setting the appearance' BackColorAlpha property 
            // or by setting the BackColor to a desired value.
            e.Layout.Override.FilterRowPromptAppearance.BackColorAlpha = Alpha.Opaque;

            // By default the prompt is spread across multiple cells if it's bigger than the
            // first cell. You can confine the prompt to a particular cell by setting the
            // SpecialRowPromptField property off the band to the key of a column.
            //e.Layout.Bands[0].SpecialRowPromptField = e.Layout.Bands[0].Columns[0].Key;

            // Display a separator between the filter row other rows. SpecialRowSeparator property 
            // can be used to display separators between various 'special' rows, including for the
            // filter row. This property is a flagged enum property so it can take multiple values.
            e.Layout.Override.SpecialRowSeparator = SpecialRowSeparator.FilterRow;

            // You can control the appearance of the separator using the SpecialRowSeparatorAppearance
            // property.
            e.Layout.Override.SpecialRowSeparatorAppearance.BackColor = Color.FromArgb(233, 242, 199);
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            string strFinRecvMoneyDetailTabID = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells[0].Text;
            m_nSelectFinRecvMoneyDetailTabID = Convert.ToInt32(strFinRecvMoneyDetailTabID, 10);
            cellFinRecvMoneyDetailTabClick();
        }

        private void cellFinRecvMoneyDetailTabClick()
        {
            DGBY.Model.FinRecvMoneyDetailTab modelFinRecvMoneyDetailTab = m_bllFinRecvMoneyDetailTab.GetModel(m_nSelectFinRecvMoneyDetailTabID);
            if (modelFinRecvMoneyDetailTab != null)
            {
                this.textBoxSeqNo.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["序号"].Text;
                this.textBoxCode.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["编号"].Text;
                this.textBoxRecvMoneyJinE.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["收款金额"].Text;
                this.textBoxZheSHuiLv.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["折算汇率"].Text;
                this.textBoxMarkedPrice.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["牌价"].Text;
                this.textBoxDiscount.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["优惠价"].Text;
                this.textBoxPayPerson.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["付款人"].Text;
                           
                this.textBoxZheSuanJinE.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["折算金额"].Text;
                this.textBoxMarkedPriceJinE.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["牌价金额"].Text;
                this.textBoxDiscountJinE.Text = this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["优惠价金额"].Text;

                this.dateTimePickerRecvMoneyDate.Value = (DateTime)this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["收款日期"].Value;
                this.dateTimePickerConfirmDate.Value = (DateTime)this.ultraGrid1.Rows[this.ultraGrid1.ActiveCell.Row.Index].Cells["认领日期"].Value;


                if (modelFinRecvMoneyDetailTab.BYCompanyTabID != null)
                    this.comboBoxBYComp.SelectedValue = modelFinRecvMoneyDetailTab.BYCompanyTabID;
                if (modelFinRecvMoneyDetailTab.BYDepartTabID != null)
                    this.comboBoxDept.SelectedValue = modelFinRecvMoneyDetailTab.BYDepartTabID;
                if (modelFinRecvMoneyDetailTab.FactoryDataTabID != null)
                    this.comboBoxFacName.SelectedValue = modelFinRecvMoneyDetailTab.FactoryDataTabID;
                if (modelFinRecvMoneyDetailTab.FinRecvMoneyWayTabID != null)
                    this.comboBoxRecvMoneyWay.SelectedValue = modelFinRecvMoneyDetailTab.FinRecvMoneyWayTabID;
                if (modelFinRecvMoneyDetailTab.BiZhiTabID != null)
                    this.comboBoxBiZhong.SelectedValue = modelFinRecvMoneyDetailTab.BiZhiTabID;
                if (modelFinRecvMoneyDetailTab.ZheSuanBiZhiTabID != null)
                    this.comboBoxZheSBiZhong.SelectedValue = modelFinRecvMoneyDetailTab.ZheSuanBiZhiTabID;
            }
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxRecvMoneyJinE))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxZheSHuiLv))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxMarkedPrice))
                    return;
                if (ClassInputValid.IsDecimal(this.errorProvider1, this.textBoxDiscount))
                    return;


                DGBY.Model.FinRecvMoneyDetailTab modelFinRecvMoneyDetailTabOld = m_bllFinRecvMoneyDetailTab.GetModel(m_nSelectFinRecvMoneyDetailTabID);
                if (modelFinRecvMoneyDetailTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.FinRecvMoneyDetailTab modelFinRecvMoneyDetailTab = new DGBY.Model.FinRecvMoneyDetailTab();
                modelFinRecvMoneyDetailTab.FinRecvMoneyDetailTabID = modelFinRecvMoneyDetailTabOld.FinRecvMoneyDetailTabID;

                modelFinRecvMoneyDetailTab.RecvMoneyValue = Convert.ToDecimal(this.textBoxRecvMoneyJinE.Text);
                modelFinRecvMoneyDetailTab.ZheSuanHuiLv = Convert.ToDecimal(this.textBoxZheSHuiLv.Text);
                modelFinRecvMoneyDetailTab.Paijia = Convert.ToDecimal(this.textBoxMarkedPrice.Text);
                modelFinRecvMoneyDetailTab.YouHuiJia = Convert.ToDecimal(this.textBoxDiscount.Text);

                modelFinRecvMoneyDetailTab.PayPerson = this.textBoxPayPerson.Text;
                modelFinRecvMoneyDetailTab.SenquenceNo = modelFinRecvMoneyDetailTabOld.SenquenceNo;

                modelFinRecvMoneyDetailTab.PaijiaJinE = Convert.ToDecimal(modelFinRecvMoneyDetailTab.RecvMoneyValue) * Convert.ToDecimal(modelFinRecvMoneyDetailTab.Paijia);
                modelFinRecvMoneyDetailTab.YouHuiJiaJinE = Convert.ToDecimal(modelFinRecvMoneyDetailTab.RecvMoneyValue) * Convert.ToDecimal(modelFinRecvMoneyDetailTab.YouHuiJia);


                if (this.comboBoxBiZhong.Text == "港币")
                {
                    modelFinRecvMoneyDetailTab.ZheSuanValue = Convert.ToDecimal(modelFinRecvMoneyDetailTab.RecvMoneyValue) * Convert.ToDecimal(modelFinRecvMoneyDetailTab.ZheSuanHuiLv);
                }
                else
                {
                    modelFinRecvMoneyDetailTab.ZheSuanValue = Convert.ToDecimal(modelFinRecvMoneyDetailTab.PaijiaJinE) / Convert.ToDecimal(modelFinRecvMoneyDetailTab.ZheSuanHuiLv);
                }


                if (this.comboBoxBYComp.SelectedValue != null)
                    modelFinRecvMoneyDetailTab.BYCompanyTabID = Convert.ToInt32(this.comboBoxBYComp.SelectedValue.ToString(), 10);
                if (this.comboBoxDept.SelectedValue != null)
                    modelFinRecvMoneyDetailTab.BYDepartTabID = Convert.ToInt32(this.comboBoxDept.SelectedValue.ToString(), 10);
                if (this.comboBoxFacName.SelectedValue != null)
                    modelFinRecvMoneyDetailTab.FactoryDataTabID = Convert.ToInt32(this.comboBoxFacName.SelectedValue.ToString(), 10);
                if (this.comboBoxRecvMoneyWay.SelectedValue != null)
                    modelFinRecvMoneyDetailTab.FinRecvMoneyWayTabID = Convert.ToInt32(this.comboBoxRecvMoneyWay.SelectedValue.ToString(), 10);
                if (this.comboBoxBiZhong.SelectedValue != null)
                    modelFinRecvMoneyDetailTab.BiZhiTabID = Convert.ToInt32(this.comboBoxBiZhong.SelectedValue.ToString(), 10);
                if (this.comboBoxZheSBiZhong.SelectedValue != null)
                    modelFinRecvMoneyDetailTab.ZheSuanBiZhiTabID = Convert.ToInt32(this.comboBoxZheSBiZhong.SelectedValue.ToString(), 10);

                modelFinRecvMoneyDetailTab.RecvMoneyDate = this.dateTimePickerRecvMoneyDate.Value.Date;
                modelFinRecvMoneyDetailTab.RenLingDate = this.dateTimePickerConfirmDate.Value.Date;
                string strRecvMoneyDate = modelFinRecvMoneyDetailTab.RecvMoneyDate.ToString();
                string strRecvMoneyDateOld = modelFinRecvMoneyDetailTabOld.RecvMoneyDate.ToString();
                if (strRecvMoneyDate.CompareTo(strRecvMoneyDateOld) == 0)
                    modelFinRecvMoneyDetailTab.BianHao = modelFinRecvMoneyDetailTabOld.BianHao;
                else
                    modelFinRecvMoneyDetailTab.BianHao = MakeBianHao(this.dateTimePickerRecvMoneyDate.Value.Date);

                m_bllFinRecvMoneyDetailTab.Update(modelFinRecvMoneyDetailTab);
                ShowAllRecord("");
                MessageBox.Show("数据修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            DGBY.Model.FinRecvMoneyDetailTab modelFinRecvMoneyDetailTabOld = m_bllFinRecvMoneyDetailTab.GetModel(m_nSelectFinRecvMoneyDetailTabID);
            if (modelFinRecvMoneyDetailTabOld == null)
            {
                MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }


            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                m_bllFinRecvMoneyDetailTab.Delete(m_nSelectFinRecvMoneyDetailTabID);

                ShowAllRecord("");
                MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.textBoxSeqNo.Text = "";
                this.textBoxCode.Text = "";
                this.textBoxRecvMoneyJinE.Text = "";
                this.textBoxZheSHuiLv.Text = "";
                this.textBoxMarkedPrice.Text = "";
                this.textBoxPayPerson.Text = "";
                this.textBoxDiscount.Text = "";
                this.textBoxZheSuanJinE.Text = "";
                this.textBoxMarkedPriceJinE.Text = "";
                this.textBoxDiscountJinE.Text = "";
                return;
            }
        }

        private void buttonExport2Excel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.ultraGridPrintDocument1.Grid = this.ultraGrid1;
            this.ultraPrintPreviewDialog1.Document = this.ultraGridPrintDocument1;
            this.ultraPrintPreviewDialog1.ShowDialog(this);
        }

        private void InitQueryCondition()
        {
            this.comboBoxLogic.SelectedIndex = 0;
            this.comboBoxQueryCondition.SelectedIndex = 0;
            this.radioButtonValueIs.Select();
            dicQueryCondition.Add("经营单位", "BYCompanyTab.CompanyName");
            dicQueryCondition.Add("序号", "SenquenceNo");
            dicQueryCondition.Add("编号", "BianHao");
            dicQueryCondition.Add("收款日期", "RecvMoneyDate");
            dicQueryCondition.Add("收款方式", "FinRecvMoneyWayTab.FinRecvMoneyBankName");
            dicQueryCondition.Add("币种", "Ba.BiZhiName");
            dicQueryCondition.Add("收款金额", "RecvMoneyValue");
            dicQueryCondition.Add("折算汇率", "ZheSuanHuiLv");
            dicQueryCondition.Add("折算币种", "Bb.BiZhiName");
            dicQueryCondition.Add("折算金额", "ZheSuanValue");
            dicQueryCondition.Add("牌价", "Paijia");
            dicQueryCondition.Add("牌价金额", "PaijiaJinE");
            dicQueryCondition.Add("优惠价", "YouHuiJia");
            dicQueryCondition.Add("优惠价金额", "YouHuiJiaJinE");
            dicQueryCondition.Add("付款人", "PayPerson");
            dicQueryCondition.Add("境外公司名称", "FactoryDataTab.FactoryName");
            dicQueryCondition.Add("部门", "BYDepartTab.DepartName");
            dicQueryCondition.Add("认领日期", "RenLingDate");
        }


        private void StartQuery(bool bIsQueryInRes)
        {
            string strWhere = "";

            string strTemp = this.comboBoxQueryCondition.SelectedItem.ToString();
            if (this.radioButtonBetween.Checked)
            {

                if (strTemp == "序号" || strTemp == "认领日期" || strTemp == "编号" || strTemp == "收款日期"
                    || strTemp == "收款金额" || strTemp == "折算汇率" || strTemp == "折算金额"
                    || strTemp == "牌价" || strTemp == "牌价金额" || strTemp == "优惠价" || strTemp == "优惠价金额")
                {
                    if (strTemp == "认领日期"|| strTemp == "收款日期")
                    {
                        if (!ValidDate(this.textBoxFrom.Text) || !ValidDate(this.textBoxTo.Text))
                        {
                            MessageBox.Show("日期格式有误，正确的格式如 20100808 !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }

                        if (this.textBoxFrom.Text != "" && this.textBoxTo.Text != "")
                        {
                            strWhere = "CONVERT(varchar(8), " + dicQueryCondition[strTemp] + ", 112) >= " + "'" + this.textBoxFrom.Text + "'" + " and CONVERT(varchar(8), " + dicQueryCondition[strTemp] + ", 112) <= " + "'" + this.textBoxTo.Text + "'";
                        }
                        else
                        {
                            MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                    else
                    {
                        if (this.textBoxFrom.Text != "" && this.textBoxTo.Text != "")
                        {
                            strWhere = dicQueryCondition[strTemp] + " BETWEEN " + "'" + this.textBoxFrom.Text + "'" + " AND " + "'" + this.textBoxTo.Text + "'";
                        }
                        else
                        {
                            MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                if (strTemp == "认领日期" || strTemp == "收款日期")
                {
                    if (this.textBoxValueIs.Text != "")
                    {
                        if (!ValidDate(this.textBoxValueIs.Text))
                        {
                            MessageBox.Show("日期格式有误，正确的格式如 20100808 !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            return;
                        }
                        strWhere = this.textBoxValueIs.Text + " LIKE CONVERT(varchar(8), " + dicQueryCondition[strTemp] + ", 112) ";
                    }
                    else
                    {
                        MessageBox.Show("输入的查询条件有错误，请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                }
                else
                {
                    strWhere = dicQueryCondition[strTemp] + " LIKE " + "'%" + this.textBoxValueIs.Text + "%'";
                }
            }

            if (bIsQueryInRes == false)
            {
                m_strQueryWhere = strWhere;
            }
            else
            {
                if (m_strQueryWhere != "")
                {
                    if (this.comboBoxLogic.SelectedIndex == 0)
                    {
                        m_strQueryWhere = "(" + m_strQueryWhere + " AND " + strWhere + ")";
                    }
                    else if (this.comboBoxLogic.SelectedIndex == 1)
                    {
                        m_strQueryWhere = "(" + m_strQueryWhere + " OR " + strWhere + ")";
                    }
                    else
                    {
                        MessageBox.Show("查询条件有误，请选择\"并且\"或\"或者\"!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                }
                else
                {
                    MessageBox.Show("没有所基于的查询结果，请首先单据\"查询\"按键!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

            }

            ShowAllRecord(m_strQueryWhere);
        }

        private void buttonQuery_Click(object sender, EventArgs e)
        {
            StartQuery(false);
        }

        private void buttonQueryFromResult_Click(object sender, EventArgs e)
        {
            StartQuery(true);
        }

        private void radioButtonValueIs_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButtonValueIs.Checked)
            {
                this.textBoxFrom.Text = "";
                this.textBoxTo.Text = "";
            }
        }

        private void radioButtonBetween_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButtonBetween.Checked)
                this.textBoxValueIs.Text = "";
        }

    }
}